home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue32 / listview / LISTVIEW.ZIP / FileHolder.pas next >
Encoding:
Pascal/Delphi Source File  |  1998-01-07  |  6.1 KB  |  255 lines

  1. unit FileHolder;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ShellAPI,
  7.   FileCtrl, Dialogs;
  8.  
  9. type
  10.  
  11.   TFileSortType =(sbName,sbDate,sbSize,sbType,sbAttr);
  12.  
  13.   TFileStat = class
  14.   private
  15.     FIcon: integer;
  16.     FHint: string;
  17.     function  getIcon: integer;
  18.     function  getHint: string;
  19.   public
  20.     Name: string;
  21.     Size: string;
  22.     Date: TDateTime;
  23.     Attr: string[4];
  24.     Fldr: Boolean;
  25.     property Icon: integer read getIcon write FIcon;
  26.     property Hint: string read getHint write FHint;
  27.   end;
  28.  
  29.   TFileHolder = class(TList)
  30.   private
  31.     FFolder: string;
  32.     FFoldersToo: Boolean;
  33.     procedure setFolder(value: string);
  34.     procedure setFoldersToo(value: Boolean);
  35.     function  GetFileStat(Index: integer): TFileStat;
  36.     procedure PutFileStat(Index: integer; FileList: TFileStat);
  37.     procedure clearList;
  38.   public
  39.     destructor Destroy; override;
  40.     procedure  readFiles;
  41.     procedure  sortFiles(sortType: TFileSortType; ascending: Boolean);
  42.     property   Files[Index: integer]: TFileStat read GetFileStat write PutFileStat;
  43.     property   Folder: string read FFolder write setFolder;
  44.     property   FoldersToo: Boolean read FFoldersToo write setFoldersToo default True;
  45.   end;
  46.  
  47. function FileCompare( Item1, Item2: Pointer): Integer;
  48.  
  49. var
  50.   FDirection: integer;
  51.   FSortType: TFileSortType;
  52.   FCurrentDir: string;
  53.   
  54. implementation
  55.  
  56. //---------------------------------------------------------------------
  57.  
  58. function TFileStat.getIcon;
  59. var
  60.   fi: TShFileInfo;
  61. begin
  62.  
  63.   if FIcon = -1 then begin
  64.     ShGetFileInfo(PChar(FCurrentDir+Name),0,fi, SizeOf(TShFileInfo), SHGFI_SYSICONINDEX );
  65.     Result := fi.iIcon;
  66.     FIcon  := fi.iIcon;
  67.   end else
  68.     Result := FIcon;
  69.  
  70. end;
  71.  
  72. //---------------------------------------------------------------------
  73.  
  74. function TFileStat.getHint;
  75. var
  76.   fi: TShFileInfo;
  77. begin
  78.  
  79.   if FHint[1] = '.' then begin
  80.     ShGetFileInfo(PChar(FCurrentDir+Name),0,fi, SizeOf(TShFileInfo), SHGFI_TYPENAME );
  81.     Result := fi.szTypeName;
  82.     FHint  := fi.szTypeName;
  83.   end else
  84.     Result := FHint;
  85.  
  86. end;
  87.  
  88. //---------------------------------------------------------------------
  89.  
  90. function TFileHolder.GetFileStat(Index: integer): TFileStat;
  91. begin
  92.   Result:=TFileStat(Items[Index]);
  93. end;
  94.  
  95. //---------------------------------------------------------------------
  96.  
  97. procedure TFileHolder.PutFileStat(Index: integer; FileList: TFileStat);
  98. begin
  99.   Items[Index]:=FileList;
  100. end;
  101.  
  102. //---------------------------------------------------------------------
  103.  
  104. procedure TFileHolder.clearList;
  105. var
  106.   i: integer;
  107. begin
  108.  
  109.   for i := Count - 1 downto 0 do
  110.     TFileStat(Items[i]).Free;
  111.   Clear;
  112.  
  113. end;
  114.  
  115. //---------------------------------------------------------------------
  116.  
  117. destructor TFileHolder.Destroy;
  118. begin
  119.  
  120.   clearList;
  121.   inherited Destroy;
  122.  
  123. end;
  124.  
  125. //---------------------------------------------------------------------
  126.  
  127. procedure TFileHolder.readFiles;
  128. var
  129.   i: integer;
  130.   fs: TFileStat;
  131.   sRec: TSearchRec;
  132.  
  133.   function AttrToStr(attr: integer): string;
  134.   begin
  135.     Result := '';
  136.     if (attr and faArchive) > 0 then
  137.       Result := Result + 'A' else Result := Result + ' ';
  138.     if (attr and faHidden) > 0 then
  139.       Result := Result + 'H' else Result := Result + ' ';
  140.     if (attr and faReadOnly) > 0 then
  141.       Result := Result + 'R' else Result := Result + ' ';
  142.     if (attr and faSysFile) > 0 then
  143.       Result := Result + 'S' else Result := Result + ' ';
  144.   end;
  145.  
  146. begin
  147.  
  148.   clearList;
  149.  
  150.   i := FindFirst(FFolder + '*.*', faDirectory, sRec);
  151.   try
  152.     while (i = 0) do begin
  153.       if ((sRec.attr and faDirectory)=0) or // a file
  154.          (FFoldersToo and ((sRec.attr and faDirectory)>0) and (sRec.Name[1] <> '.')) then begin
  155.         fs := TFileStat.Create;
  156.         fs.Name := sRec.Name;
  157.         fs.Date := FileDateToDateTime(sRec.Time);
  158.         fs.Attr := AttrToStr(sRec.Attr);
  159.         fs.Icon := -1;
  160.         fs.Hint := '.'; 
  161.         fs.Fldr := (sRec.Attr and faDirectory)>0;
  162.         if not fs.Fldr then 
  163.           fs.Size := Format( '%.0n', [sRec.Size+0.0] )
  164.         else
  165.           fs.Size := '';
  166.         Add( fs );
  167.       end;
  168.       i := FindNext(sRec);
  169.       Application.ProcessMessages;
  170.     end;
  171.   finally
  172.     SysUtils.FindClose(sRec);
  173.   end;
  174.  
  175. end;
  176.  
  177. //------------------------------------------------------------------
  178.  
  179. function FileCompare(item1, item2: Pointer): integer;
  180. var
  181.   comp1, comp2: string;
  182.  
  183.   function iif(TF: Boolean; ifT, ifF: string): string;
  184.   begin
  185.     if TF then Result := ifT else Result := ifF;
  186.   end;
  187.  
  188. begin
  189.  
  190.   case FSortType of
  191.     sbName: begin
  192.       comp1 := TFileStat(Item1).Name;
  193.       comp2 := TFileStat(Item2).Name;
  194.     end;
  195.     sbDate: begin
  196.       comp1 := Format( '%0.3f', [TFileStat(Item1).Date] );
  197.       comp2 := Format( '%0.3f', [TFileStat(Item2).Date] );
  198.     end;
  199.     sbSize: begin
  200.       comp1 := Format( '%9.9s', [TFileStat(Item1).Size] );
  201.       comp2 := Format( '%9.9s', [TFileStat(Item2).Size] );
  202.     end;
  203.     sbType: begin
  204.       comp1 := TFileStat(Item1).Hint;
  205.       comp2 := TFileStat(Item2).Hint;
  206.     end;
  207.     sbAttr: begin
  208.       comp1 := TFileStat(Item1).Attr;
  209.       comp2 := TFileStat(Item2).Attr;
  210.     end;
  211.   end;
  212.  
  213.   comp1 := iif(TFileStat(Item1).Fldr,'!','~') + comp1;
  214.   comp2 := iif(TFileStat(Item2).Fldr,'!','~') + comp2;
  215.   Result := FDirection * CompareText( comp1, comp2 );
  216.  
  217. end;
  218.  
  219. //---------------------------------------------------------------------
  220.  
  221. procedure TFileHolder.sortFiles(sortType: TFileSortType; ascending: Boolean);
  222. begin
  223.  
  224.   FSortType := sortType;
  225.   if ascending then
  226.     FDirection := 1
  227.   else
  228.     FDirection := -1;
  229.   Sort(FileCompare);    
  230.  
  231. end;
  232.  
  233. //---------------------------------------------------------------------
  234.  
  235. procedure TFileHolder.setFolder(value: string);
  236. begin
  237.  
  238.   FFolder := value;
  239.   if FFolder[Length(FFolder)] <> '\' then
  240.     FFolder := FFolder + '\';
  241.   FCurrentDir := FFolder;
  242.  
  243. end;
  244.  
  245. //---------------------------------------------------------------------
  246.  
  247. procedure TFileHolder.setFoldersToo(value: Boolean);
  248. begin
  249.  
  250.   FFoldersToo := value;
  251.  
  252. end;
  253.  
  254. end.
  255.